API_URL = undefined
HOST = "api.xvc.com"

ROUTE = {
    subscribers:->
        ing = undefined
        id = "mail-subscribers"
        if $("##{id}")[0]
            return
        btn = $ """<a class="gh-btn gh-btn-blue ember-view" id="#{id}" href="#"><span>群发邮件</span></a>"""
        $(".view-actions").prepend btn
        btn.click ->
            if ing
                return
            ing = 1
            $.getJSON("#{API_URL}configuration/").then ({configuration})->

                ghost.init configuration[0]
                post_li = await $.getJSON(ghost.url.api('posts', {limit: 1, fields:"title,url,custom_excerpt"}))
                li = []
                for i in post_li.posts
                    i.url = location.protocol+"//"+location.host+i.url
                    box = $ '<div><input><textarea></textarea><button>群发订阅提醒邮件</button></div>'
                    input = box.find('input').val(document.title.split("-").pop().trim() + " 更新提醒 : "+i.title)
                    if i.custom_excerpt
                        brief = i.custom_excerpt+"\n\n"
                    else
                        brief = ""

                    textarea = box.find('textarea').val("请大家关注我们的更新 《"+i.title+"》\n\n#{brief}"+"全文链接 : "+i.url)
                    fancybox = $.fancybox.open(box,{
                        smallBtn:false
                        clickOutside:false
                        clickSlide:false
                        touch:false
                        buttons:['close']
                        afterClose:->
                            ing = undefined
                        afterShow:->
                            box.find('button').click =>
                                title = input.val()
                                txt = textarea.val()
                                @$slide.parents('.fancybox-inner').find('.fancybox-button--close').remove()
                                @$content.html(
                                    """<div style="text-align:center;font-size:18px"><img src="//qiniu.xvc.com/spin.svg" style="vertical-align:-13px;"> 「#{_.escape title}」发送中</div>"""
                                )
                                $.post(
                                    "https://#{HOST}/mail/send"
                                    {
                                        url:"#{API_URL}subscribers/csv?access_token=#{JSON.parse(localStorage.getItem("ghost:session")).authenticated.access_token}"
                                        title
                                        txt
                                    }
                                    =>
                                        sended = $ """<div style="text-align:center"><h1 style="font-size:21px"><p>「#{_.escape title}」</p><p style="color:#519703">发送成功</p> </h1><button>干得不错 ！</button></div>"""
                                        @$content.html sended
                                        sended.find('button').click -> fancybox.close()
                                    "json"
                                )

                                false

                    })

                    break
            false

}

route = ->
    setTimeout(
        ->
            action = location.hash.slice(2).split("?")[0]
            ROUTE[action]?()
        300
    )

$.getScript("/public/ghost-sdk.min.js").then ->
    API_URL = ghost.url.api()
    $ route
    $(window).on 'hashchange', route
